#include "visbasehashcontainer.h"
#include "visbasecontainer.h"

VisBaseHashContainer::VisBaseHashContainer(QPointer<SageVis> sageVisPointer, QPointer<VisBaseWidget> parentWidget, QGraphicsItem *parent ) :
    VisBaseContainer( sageVisPointer, parentWidget, parent )
{

}

void VisBaseHashContainer::clearAndDelete()
{
    qDebug() << "emptying hash container" << endl;
    super::clearAndDelete();
    //qDeleteAll(hashTable);
    hashTable.clear();

    qDebug() << "hastable empty? " << hashTable.isEmpty() << " size: " << hashTable.size() << " num values " << hashTable.values().size() << " num keys " << hashTable.keys().size() << endl;

}


void VisBaseHashContainer::addElement(QPointer< VisBaseElement > e, QString hashKey)
{
    hashTable.insert(hashKey, e);
    super::addElement(e);
}


void VisBaseHashContainer::addElement(QPointer< VisBaseElement > e)
{
    super::addElement(e);
}

void VisBaseHashContainer::hashElement(QPointer< VisBaseElement > e, QString hashKey)
{
    hashTable.insert(hashKey, e);
}

QList< QPointer<VisBaseElement> > VisBaseHashContainer::getElements(QString hashKey)
{
    return hashTable.values( hashKey );
}

bool VisBaseHashContainer::removeElement(int idx)
{
    QString hashKey = hashTable.key( elements[idx] );
    hashTable.remove(hashKey);
    super::removeElement(idx);
}

bool VisBaseHashContainer::removeElement(QString key)
{
    int idx = hashedElements.value( key );//get idx

    QString hashKey = hashTable.key( elements[idx] );//look up element
    hashTable.remove(hashKey);//remove it
    super::removeElement(key);//super::remove element
}


void VisBaseHashContainer::connectElementsWithSameKey(int type)
{
    QList<QString> keys = hashTable.uniqueKeys();//get all the keys
    qDebug() << "keys " << keys.size();
    qDebug() << "elements " << elements.size();

    float aveEltsPerKey = 0.0;

    for(int i = 0; i < keys.size(); i++)//for all unique keys
    {
        QList< QPointer< VisBaseElement > > elts = hashTable.values( keys[i] );//get list of elements with that key
        aveEltsPerKey += elts.size();
        for(int j = 0; j < elts.size(); j++) //for all the elements with this key
        {
            for(int k = j+1; k < elts.size(); k++) //loop through all other elements with this key
            {
                //forgeConnection( elts[j], elts[k], type ); //connect these elements together
                elts[j]->forgeConnection( elts[k], type);//HOVER_CONNECTION );
                elts[k]->forgeConnection( elts[j], type);//HOVER_CONNECTION );
            }
        }
    }

    aveEltsPerKey /= (float)keys.size();

    qDebug() << "done " << aveEltsPerKey << endl;
}

void VisBaseHashContainer::connectElementsWithSameKey(int type, QString key)
{
    QList< QPointer< VisBaseElement > > elts = hashTable.values( key );//get list of elements with that key
    for(int j = 0; j < elts.size(); j++) //for all the elements with this key
    {
        for(int k = j+1; k < elts.size(); k++) //loop through all other elements with this key
        {
            //forgeConnection( elts[j], elts[k], type ); //connect these elements together
            elts[j]->forgeConnection( elts[k], type);//HOVER_CONNECTION );
            elts[k]->forgeConnection( elts[j], type);//HOVER_CONNECTION );
        }
    }
}
